package com.uenpay.oem.ui.user.login;

import android.app.Activity;
import android.text.TextUtils;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.ui.old.activity.login.LoginAndRegisterHandle;

/**
 * Created by liulou on 2017/5/9.
 * desc:
 */

public class LoginPresenter implements LoginContract.Present {

    public static final int STATE_LOGIN_SUCCESS = 1;
    public static final int STATE_LOGIN_FAIL = -1;
    public static final int STATE_LOGIN_OTHER = 0;


    private LoginContract.View mLoginView;
    private LoginAndRegisterHandle loginAndRegisterHandle;

    public LoginPresenter(LoginContract.View loginView) {
        this.mLoginView = loginView;
        this.loginAndRegisterHandle = new LoginAndRegisterHandle();
    }

    @Override
    public void login(String userName, String pwd) {
        boolean isNetworkAvailable = Utils
                .isNetworkConnected(IMApplication.getAppContext());
        if (!isNetworkAvailable) {
            mLoginView.showToast(getString(R.string.no_network));
            return;
        }
        if (!TextUtils.isEmpty(userName) || !TextUtils.isEmpty(pwd)) {
            if(TextUtils.isEmpty(userName.trim())){
                mLoginView.showToast(getString(R.string.phone_not_null));
                return ;
            }
            if (TextUtils.isEmpty(pwd.trim())) {
                mLoginView.showToast(getString(
                        R.string.pwd_not_null));
                return;
            }

            if (pwd.getBytes().length != pwd.length()) {
                mLoginView.showToast(getString(
                        R.string.pwd_can_not_contain_chinese_characters));
                return;
            }
            boolean mat = pwd.matches("^(\\s|.*\\s+.*)$");
            if (mat) {
                mLoginView.showToast(getString(
                        R.string.pwd_can_not_contain_space));
                return;
            }
            if (checkPwdLength(pwd)) {
                startLogin(userName, pwd);
            } else {
                mLoginView.showToast(getString(
                        R.string.pwd_length));
            }
        } else {
            mLoginView.showToast(getString(
                    R.string.phone_and_pwd_not_null));
        }
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDestroy() {

    }

    private String getString(int res) {
        return ((Activity) mLoginView).getResources().getString(res);
    }

    /**
     * 检查密码长度
     *
     * @param pwd
     * @return
     */
    private boolean checkPwdLength(String pwd) {
        if (pwd.length() < 6 || pwd.length() > 16) {
            return false;
        } else {
            return true;
        }
    }

    private void startLogin(final String account, final String pwd) {
        mLoginView.startLogin();
        loginAndRegisterHandle.loginApp(account, pwd,
                new LoginAndRegisterHandle.ILoginResultListener() {

                    @Override
                    public void onLoginResult(int status, String msg,
                                              QYXUserEntity mQyxUserEntity) {
                        int state = 0;
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            if (mQyxUserEntity != null
                                    && (mQyxUserEntity.status == 1 || mQyxUserEntity.status == HttpInvokeResultCode.RESULT_OK)) {
                                state = STATE_LOGIN_SUCCESS;
                            } else {
                                if (!TextUtils.isEmpty(msg)) {
                                    mLoginView.showToast(msg);
                                    state = STATE_LOGIN_OTHER;
                                }
                            }
                        } else {
                            state = STATE_LOGIN_FAIL;
                        }
                        mLoginView.completeLogin(state);
                    }
                });
    }
}
